// pages/pileVersion/pileVersion.js
const app = getApp();
const util = require('../../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    pileList: [],
    singlePileShow: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中...',
    })
    let that = this
    let hospitalId = options.hospitalId
    let hospitalName = options.hospitalName
    wx.setNavigationBarTitle({
      title: hospitalName,
    })
    wx.request({
      url: util.CONST_DATA.BASE_URL + 'district/queryPileInfo?hospitalId=' + hospitalId,
      header: app.globalData.header,
      method: 'POST',
      success: function (res) {
        wx.hideLoading()
        console.log('【医院的桩位信息>>】', res)
        that.setData({
          hospitalsShow: false,
          pileList: res.data
        })
        that.resort()
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },
  resort: function(){
    let that = this
    let map = {},
      dest = [];
    let pileList = that.data.pileList
    for (let i = 0; i < pileList.length; i++) {
      var pileItem = pileList[i];
      if (!map[pileItem.hpPosition]) {
        dest.push({
          hpPosition: pileItem.hpPosition,
          checked: true,
          data: [pileItem]
        });
        map[pileItem.hpPosition] = pileItem;
      } else {
        for (let j = 0; j < dest.length; j++) {
          var dj = dest[j];
          if (dj.hpPosition == pileItem.hpPosition) {
            dj.data.push(pileItem);
            break;
          }
        }
      }
    }
    console.log("dest>>>", dest);
    that.setData({
      resortList: dest,
      choosedList: dest,
      searchedPiles: dest
    })
  },
  checkboxChange: function(e){
    let that = this
    let resortList = that.data.resortList
    let choosedList = []
    for (let i = 0; i < resortList.length; i++){
      for (let j = 0; j < e.detail.value.length;j++){
        if (resortList[i].hpPosition == e.detail.value[j]){
          choosedList.push(resortList[i])
        }
      }
    }
    that.setData({
      choosedList: choosedList,
      searchedPiles: choosedList
    })
    if(that.data.inputPile){
      that.searchPile()
    }
  },
  inputPile: function (e) {
    this.setData({
      inputPile: e.detail.value
    });
    this.searchPile()
  },
  searchPile: function(){
    let searchPile = this.data.inputPile;
    let choosedList = this.data.choosedList;
    var piles = [];
    for (let i = 0; i < choosedList.length; i++) {
      for (let j = 0; j < choosedList[i].data.length; j++) {
        if (choosedList[i].data[j].pileCode.indexOf(searchPile) != -1) {
          piles[i] = {
            hpPosition: choosedList[i].hpPosition,
            checked: true,
            data: []
          }
          piles[i].data.push(choosedList[i].data[j]);
        }
      }
    }
    if (searchPile == '') {
      piles = this.data.choosedList;
    }
    this.setData({
      searchedPiles: piles
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})